//
//  PQHistoryBarCodeTableViewController.m
//  PinQiao
//
//  Created by 老贾 on 16/11/3.
//  Copyright © 2016年 jiayaning. All rights reserved.
//

#import "PQHistoryBarCodeTableViewController.h"
#import "PQHistoryBarCodeTableViewCell.h"
#import "PQHistoryMenu.h"
#define BarCodeHistory [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"history.plist"]

@interface PQHistoryBarCodeTableViewController () <CCResultViewControllerDelegate>

/**模型数组*/
@property (nonatomic, strong) NSMutableArray *DataList;
/**Loading*/
@property (nonatomic, strong) UIImageView *LoadingImage;

@end

static NSString *Identifier = @"cell";

@implementation PQHistoryBarCodeTableViewController

- (NSMutableArray *)DataList{
    if (!_DataList) {
        _DataList = [NSMutableArray array];
    }
    return _DataList;
}

- (void)viewWillAppear:(BOOL)animated{
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.navigationItem.title = @"扫描历史";
}

- (void)viewDidDisappear:(BOOL)animated{

    [self.delegate startAni];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    
    [self loadData];
}

- (void)initUI{
    self.view.backgroundColor = RGBColor(243, 245, 247);
    self.tableView.separatorColor = [UIColor clearColor];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"PQHistoryBarCodeTableViewCell" bundle:nil] forCellReuseIdentifier:Identifier];
    
    /**初始化清除按钮，导航栏右侧按钮，一键清除所有数据*/
    UIBarButtonItem *items = [[UIBarButtonItem alloc]initWithTitle:@"清除" style:UIBarButtonItemStyleDone target:self action:@selector(buttonClear:)];
    UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    space.width = -8;
    if (WIDTH == 414) {
        self.navigationItem.rightBarButtonItems = @[space,items];
    }else{
        self.navigationItem.rightBarButtonItem = items;
    }
}

#pragma mark - 导航栏清除按钮点击事件
- (void)buttonClear:(UIBarButtonItem *)item{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定要清除全部历史记录吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"点错了" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *OKaction = [UIAlertAction actionWithTitle:@"清空" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.DataList removeAllObjects];
        [self.tableView reloadData];
        [self NotValueAddIamgeWith:YES];
        [self.delegate removeAllobjects];
        NSFileManager *filemanager = [NSFileManager defaultManager];
        [filemanager removeItemAtPath:BarCodeHistory error:nil];
    }];
    [alert addAction:cancle];
    [alert addAction:OKaction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)loadData{
    
    if (self.BarCodeArray.count != 0) {
        
        [self NotValueAddIamgeWith:NO];
        for (NSDictionary *dic in self.BarCodeArray) {
            PQHistoryMenu *history = [[PQHistoryMenu alloc]init];
            [history setValuesForKeysWithDictionary:dic];
            [self.DataList addObject:history];
        }
    }else{
        [self NotValueAddIamgeWith:YES];
    }
}

- (void)NotValueAddIamgeWith:(BOOL)notValue{
    
    if (notValue) {
        _LoadingImage = [[UIImageView alloc]initWithFrame:CGRectMake( 0, 0, WIDTH*0.4, WIDTH*0.4)];
        _LoadingImage.cc_centerX = WIDTH/2;
        _LoadingImage.cc_y = WIDTH*0.25;
        //_LoadingImage.image = [UIImage imageWithContentsOfFile:image(@"nohistory")];
        
        _LoadingImage.image = Image(@"nohistory");
        
        [self.view addSubview:_LoadingImage];
    }else{
        [_LoadingImage removeFromSuperview];
    }
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.DataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PQHistoryBarCodeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    cell.historyMenu = self.DataList[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
}

@end
